/* * * * M A P V I S I O N   L I C E N S E  *****************************************************************
 *
 * This MapVision software is © Filip Zavadil 2013. It is licensed under the LGPL license.
 * This MapVision software dynamically links to unmodified Qt5 Library.
 *
 * The Qt5 Library is © 2013 Qt® and/or its subsidiary(-ies), and is licensed under the
 * GNU Lesser General Public License version 2.1 with Qt® LGPL exception version 1.1.
 * Qt5 library is free software; you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
 * Qt5 library is provided “AS IS”, without WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT,
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 *
 * As an additional permission to the GNU Lesser General Public License version 2.1, the object
 * code form of a “work that uses the Library” may incorporate material from a header file that is
 * part of the Library. You may distribute such object code under terms of your choice,
 * provided that: (i) the header files of the Library have not been modified; and (ii)
 * the incorporated material is limited to numerical parameters, data structure layouts, accessors,
 * macros, inline functions and templates; and (iii) you comply with the terms of Section 6 of the
 * GNU Lesser General Public License version 2.1.
 *
 * Moreover, you may apply this exception to a modified version of the Library, provided that such
 * modification does not involve copying material from the Library into the modified Library’s header
 * files unless such material is limited to (i) numerical parameters; (ii) data structure layouts;
 * (iii) accessors; and (iv) small macros, templates and inline functions of five lines or less in length.
 *
 * Furthermore, you are not required to apply this additional permission to a modified version of the Library.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this package;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

 * The source code for Qt 5.X SDK is available from Qt Project here:
 * http://download.qt-project.org/official_releases/qtcreator/2.7/2.7.0/qt-creator-2.7.0-src.zip.
 *
 * ****************************** M A P V I S I O N    S O F T W A R E  ************************************
 *
 * Author:       Filip Zavadil
 * Description:  Software for displaying and exporting map tiles.
 * The sources and executables of MapVision software are available:
 *    - from project hosting on CodePlex.com :   http://mapvision.codeplex.com
 *    - from GIT repositories on Gitorious.org:  https://gitorious.org/mapvision
 * Visit MapVision fanpage on Facebook.com:      https://www.facebook.com/pages/Mapvision/631827753513935
 *
 * *********************************************************************************************************
*/

#ifndef MAPVIEW_H
#define MAPVIEW_H

#include "mapscene.h"
#include "zavaproj.h"
#include <QGraphicsView>
#include <QMouseEvent>
#include <QResizeEvent>
#include <cmath>
#include <QDebug>
#include "welcome.h"



#include "selectionrubberband.h"

class MapView : public QGraphicsView
{
    Q_OBJECT

public:
    enum MapViewMode { PAN, SELECT_AREA, SELECT_ITEMS, POINTER };

private:
    // zoom level
    int zoom;
    //mapScene - viewPort fot tiled data
    MapScene *scene;
    //Holds the current centerpoint for the view, used for panning and zooming
    QPointF currentCenterPoint;

    QPointF lastPanPoint;
    // setter/getter of center point in viewport coordinate system
    void setCenterPixels(const QPointF centerPoint);
    QPointF getCenterPixels() { return currentCenterPoint; }
    void scaleView(qreal scaleFactor);
    ZavaProj zavaProj;
    void redraw();

    // bool value if scene is moving
    bool panEnabled; // implicit true

    // holds the max rect
    QRect rectMax;

    // print WELCOME screen
    Welcome * welcome;



public:
    // Constructor
    MapView(QWidget *parent = NULL);

    // mouse mode
    MapViewMode mapViewMode;

    // returning map scene (to add items)
    MapScene* getScene(){return scene;}

    // setter/getter center point in WGS84 coordinate system
    void setCenterLonLat(QPointF lonLat);
    void setCenterLonLat(QString dmsFormat);
    QPointF getCenterLonLat();

    // setter/getter zoom level
    void setZoom(int zoom_);
    int getZoom(){return zoom;}

    // setter/getter view scale, view zoom
    void setViewScale(int viewScale);
    int getViewScale(){return int(this->transform().m11()*100);}


    // map scale - human readable map scale
    QString getMapScale();

    // update - this method is call, when the map is moved
    void update();

    // refresh - clean scene and render new tiles
    void refresh();

    // set makeSelection
    void setSelection(bool isSelecting){ selection->isActive(isSelecting); panEnabled = !isSelecting;}

    // rect of mapView in WGS84
    QRectF getRectLonlat();

    // mouse events
    virtual void wheelEvent(QWheelEvent *event);
    virtual void mousePressEvent(QMouseEvent* event);
    virtual void mouseMoveEvent(QMouseEvent* event);
    virtual void mouseReleaseEvent(QMouseEvent *event);
    virtual void mouseDoubleClickEvent(QMouseEvent *event);

    bool viewportEvent(QEvent *event);

    // last cursor
    QCursor lastCursor;

    // SELECTION_AREA
    SelectionRubberBand * selection;

    void scrollContentsBy(int dx, int dy);

    // key events
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void keyReleaseEvent(QKeyEvent *event);

    // resize event
    virtual void resizeEvent(QResizeEvent *event);

    // setting visibility to layers
    void tilesVisible(QString layerName, bool isVisible);

    // setting panEnabled value
    void setPanEnabled(bool enabled){panEnabled = enabled; emit panEnabledChanged(enabled);}
    bool getPanEnabled(){return panEnabled;}

    // handle welcome screen
    Welcome* getWelcome(){return welcome;}

signals:
    void zoomChanged(int);
    void centerPointChanged(QPointF);
    void viewScaleChanged(int);
    void mouseOnLonlat(QString);
    void mapRectChanged(QRect,int); // emiting when scene was moved and new tiles have to be rendered
    void message(QString); // send message to console log
    void mapUpdated(QPointF, int, int);
    void clickOnLonlat(QPointF);
    void sceneCleared();

    void mouseMoved(QMouseEvent *event);
    void mouseReleased(QMouseEvent *event);
    void mousePressed(QMouseEvent *event);

    void panEnabledChanged(bool);

    // emit when item was selected (must by implement in QGraphicsItem)
    void itemSelected(QGraphicsItem*);

    void isUpdated();

private slots:
    void zoomHasChanged(int);

public slots:
    void sendMessagePublic(QString message_){emit message(message_);}
    void itemWasSelected(QGraphicsItem*);


};

#endif // MAPVIEW_H

